
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <title>updateHostRoot · React源码解析</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.0">
        
        
        
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-theme-api/theme-api.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

         
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="update-host-component.html" />
    
    
    <link rel="prev" href="update-class-component.html" />
    

<script
  async
  src="https://www.googletagmanager.com/gtag/js?id=UA-49483742-5"
></script>
<script>
  window.dataLayer = window.dataLayer || []
  function gtag() {
    dataLayer.push(arguments)
  }
  gtag('js', new Date())

  gtag('config', 'UA-49483742-5')
</script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    首页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    基础
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../api/react.html">
            
                <a href="../../api/react.html">
            
                    
                    React API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../api/react-element.html">
            
                <a href="../../api/react-element.html">
            
                    
                    ReactElement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../api/react-children.html">
            
                <a href="../../api/react-children.html">
            
                    
                    React Children
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../api/react-structure.html">
            
                <a href="../../api/react-structure.html">
            
                    
                    React 中的数据结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    创建更新
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../update/react-dom-render.html">
            
                <a href="../../update/react-dom-render.html">
            
                    
                    ReactDOM.render
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../update/set-state.html">
            
                <a href="../../update/set-state.html">
            
                    
                    setState & forceUpdate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../../update/expiration-time.html">
            
                <a href="../../update/expiration-time.html">
            
                    
                    expirationTime 计算
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    任务调度
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../scheduler-global.html">
            
                <a href="../scheduler-global.html">
            
                    
                    全局变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../schedule-work.html">
            
                <a href="../schedule-work.html">
            
                    
                    scheduleWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../scheduler-pkg.html">
            
                <a href="../scheduler-pkg.html">
            
                    
                    react-scheduler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../perform-work.html">
            
                <a href="../perform-work.html">
            
                    
                    performWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../perform-unit-of-work.html">
            
                <a href="../perform-unit-of-work.html">
            
                    
                    performUnitOfWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../render-root.html">
            
                <a href="../render-root.html">
            
                    
                    renderRoot
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.6.1" data-path="../render-root/throw-exception.html">
            
                <a href="../render-root/throw-exception.html">
            
                    
                    throwException
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../begin-work.html">
            
                <a href="../begin-work.html">
            
                    
                    beginWork
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.7.1" data-path="mount-indeterminate-component.html">
            
                <a href="mount-indeterminate-component.html">
            
                    
                    mountIndeterminateComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.2" data-path="mount-lazy-component.html">
            
                <a href="mount-lazy-component.html">
            
                    
                    mountLazyComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.3" data-path="update-functional-component.html">
            
                <a href="update-functional-component.html">
            
                    
                    updateFunctionalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.4" data-path="update-class-component.html">
            
                <a href="update-class-component.html">
            
                    
                    updateClassComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.7.5" data-path="update-host-root.html">
            
                <a href="update-host-root.html">
            
                    
                    updateHostRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.6" data-path="update-host-component.html">
            
                <a href="update-host-component.html">
            
                    
                    updateHostComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.7" data-path="update-host-text.html">
            
                <a href="update-host-text.html">
            
                    
                    updateHostText
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.8" data-path="../../feature/Suspense-component.md">
            
                <span>
            
                    
                    updateSuspenseComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.9" data-path="update-portal-component.html">
            
                <a href="update-portal-component.html">
            
                    
                    updatePortalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.10" data-path="update-forward-ref.html">
            
                <a href="update-forward-ref.html">
            
                    
                    updateForwardRef
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.11" data-path="update-fragment.html">
            
                <a href="update-fragment.html">
            
                    
                    updateFragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.12" data-path="update-mode.html">
            
                <a href="update-mode.html">
            
                    
                    updateMode
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.13" data-path="update-profiler.html">
            
                <a href="update-profiler.html">
            
                    
                    updateProfiler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.14" data-path="update-context-provider.html">
            
                <a href="update-context-provider.html">
            
                    
                    updateContextProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.15" data-path="update-context-consumer.html">
            
                <a href="update-context-consumer.html">
            
                    
                    updateContextConsumer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../reconcile-children.html">
            
                <a href="../reconcile-children.html">
            
                    
                    reconcileChildren
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.8.1" data-path="../reconcile-children/single.html">
            
                <a href="../reconcile-children/single.html">
            
                    
                    调和单个节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8.2" data-path="../reconcile-children/array.html">
            
                <a href="../reconcile-children/array.html">
            
                    
                    调和列表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    commit 阶段
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../../commit-phase/commit-root.html">
            
                <a href="../../commit-phase/commit-root.html">
            
                    
                    commitRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../../commit-phase/invokeGuardedCallback.html">
            
                <a href="../../commit-phase/invokeGuardedCallback.html">
            
                    
                    invokeGuardedCallback
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../../commit-phase/commitBeforeMutationLifecycles.html">
            
                <a href="../../commit-phase/commitBeforeMutationLifecycles.html">
            
                    
                    commitBeforeMutationLifecycles
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../../commit-phase/commit-all-host-effects.html">
            
                <a href="../../commit-phase/commit-all-host-effects.html">
            
                    
                    commitAllHostEffects
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.4.1" data-path="../../commit-phase/host-effects/CommitPlacement.html">
            
                <a href="../../commit-phase/host-effects/CommitPlacement.html">
            
                    
                    commitPlacement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.2" data-path="../../commit-phase/host-effects/CommitWork.html">
            
                <a href="../../commit-phase/host-effects/CommitWork.html">
            
                    
                    commitWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.3" data-path="../../commit-phase/host-effects/CommitDeletion.html">
            
                <a href="../../commit-phase/host-effects/CommitDeletion.html">
            
                    
                    commitDeletion
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../../commit-phase/commit-all-lifecycles.html">
            
                <a href="../../commit-phase/commit-all-lifecycles.html">
            
                    
                    commitAllLifecycle
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    功能
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../features/context.html">
            
                <a href="../../features/context.html">
            
                    
                    context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../features/hydrate.html">
            
                <a href="../../features/hydrate.html">
            
                    
                    hydrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../../features/ref.html">
            
                <a href="../../features/ref.html">
            
                    
                    ref
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../../features/event-injection.html">
            
                <a href="../../features/event-injection.html">
            
                    
                    事件系统初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../../features/event-bind.html">
            
                <a href="../../features/event-bind.html">
            
                    
                    事件绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../../features/event-dispatch.html">
            
                <a href="../../features/event-dispatch.html">
            
                    
                    事件触发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../../features/event-create-event-object.html">
            
                <a href="../../features/event-create-event-object.html">
            
                    
                    事件对象生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../../features/Suspense-component.html">
            
                <a href="../../features/Suspense-component.html">
            
                    
                    Suspense
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../../features/suspense.html">
            
                <a href="../../features/suspense.html">
            
                    
                    挂起任务的含义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../../features/suspense-lazy.html">
            
                <a href="../../features/suspense-lazy.html">
            
                    
                    lazy 组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    Hooks
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../../hooks/hooks-start.html">
            
                <a href="../../hooks/hooks-start.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../../hooks/hooks-use-state.html">
            
                <a href="../../hooks/hooks-use-state.html">
            
                    
                    useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../../hooks/hooks-use-effect.html">
            
                <a href="../../hooks/hooks-use-effect.html">
            
                    
                    useEffect
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../../hooks/hooks-others.html">
            
                <a href="../../hooks/hooks-others.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../../hooks/hooks-common.html">
            
                <a href="../../hooks/hooks-common.html">
            
                    
                    通用API
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <form
            action="https://buttondown.email/api/emails/embed-subscribe/Jokcy"
            method="post"
            target="popupwindow"
            onsubmit="window.open('https://buttondown.email/Jokcy', 'popupwindow')"
            class="buttondown-form"
            >
            <input type="email" name="email" id="bd-email" placeholder="(your@email.com)">
            <input type="hidden" value="1" name="embed"/>
            <button type="submit">订阅我</button>
        </form>
    </li>
</ul>

                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                 
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >updateHostRoot</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="updatehostroot">updateHostRoot</h1><div class="api-method"><div class="api-method-definition"><p>&#x5728;<code>scheduleRootUpdate</code>&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x4E3A;<code>root</code>&#x521B;&#x5EFA;<code>update</code>&#xFF0C;&#x5305;&#x62EC;&#x7684;&#x6700;&#x4E3B;&#x8981;&#x4FE1;&#x606F;&#x662F;<code>payload</code>&#x4E2D;&#x7684;<code>element</code></p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">scheduleRootUpdate</span>(<span class="hljs-params">current, element, expirationTime, callback</span>) </span>{
  {
    <span class="hljs-keyword">if</span> (ReactDebugCurrentFiber.phase === <span class="hljs-string">&apos;render&apos;</span> &amp;&amp; ReactDebugCurrentFiber.current !== <span class="hljs-literal">null</span> &amp;&amp; !didWarnAboutNestedUpdates) {
      didWarnAboutNestedUpdates = <span class="hljs-literal">true</span>;
      warning(<span class="hljs-literal">false</span>, <span class="hljs-string">&apos;Render methods should be a pure function of props and state; &apos;</span> + <span class="hljs-string">&apos;triggering nested component updates from render is not allowed. &apos;</span> + <span class="hljs-string">&apos;If necessary, trigger nested updates in componentDidUpdate.\n\n&apos;</span> + <span class="hljs-string">&apos;Check the render method of %s.&apos;</span>, getComponentName(ReactDebugCurrentFiber.current) || <span class="hljs-string">&apos;Unknown&apos;</span>);
    }
  }

  <span class="hljs-keyword">var</span> update = createUpdate(expirationTime);
  <span class="hljs-comment">// Caution: React DevTools currently depends on this property</span>
  <span class="hljs-comment">// being called &quot;element&quot;.</span>
  update.payload = { element: element };

  callback = callback === <span class="hljs-literal">undefined</span> ? <span class="hljs-literal">null</span> : callback;
  <span class="hljs-keyword">if</span> (callback !== <span class="hljs-literal">null</span>) {
    !(<span class="hljs-keyword">typeof</span> callback === <span class="hljs-string">&apos;function&apos;</span>) ? warning(<span class="hljs-literal">false</span>, <span class="hljs-string">&apos;render(...): Expected the last optional `callback` argument to be a &apos;</span> + <span class="hljs-string">&apos;function. Instead received: %s.&apos;</span>, callback) : <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>;
    update.callback = callback;
  }
  enqueueUpdate(current, update, expirationTime);

  scheduleWork$<span class="hljs-number">1</span>(current, expirationTime);
  <span class="hljs-keyword">return</span> expirationTime;
}</code></pre>
<p><code>ensureWorkInProgressQueueIsAClone</code>&#x662F;&#x786E;&#x4FDD;&#x4E00;&#x4E0B;<code>workInProgress</code>&#x662F;<code>current</code>&#x7684;&#x4E00;&#x4E2A;&#x62F7;&#x8D1D;&#xFF0C;&#x786E;&#x4FDD;&#x4E0D;&#x5728;<code>current</code>&#x4E0A;&#x76F4;&#x63A5;&#x64CD;&#x4F5C;</p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">resetHydrationState</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (!supportsHydration) {
    <span class="hljs-keyword">return</span>;
  }

  hydrationParentFiber = <span class="hljs-literal">null</span>;
  nextHydratableInstance = <span class="hljs-literal">null</span>;
  isHydrating = <span class="hljs-literal">false</span>;
}</code></pre>
<p>&#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E9B;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x5E94;&#x8BE5;&#x662F;&#x540E;&#x9762;&#x8FDB;&#x884C;<code>hydrate</code>&#x8981;&#x7528;&#x5230;</p><p>&#x6700;&#x540E;<code>return</code>&#x7684;&#x662F;<code>child</code>&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;<code>child</code>&#x5C31;&#x5B8C;&#x6210;&#x5DE5;&#x4F5C;&#x4E86;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x8BF4;&#x660E;&#x8FD8;&#x6CA1;&#x5B8C;&#x6210;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateHostRoot</span>(<span class="hljs-params">current, workInProgress, renderExpirationTime</span>) </span>{
  pushHostRootContext(workInProgress);
  <span class="hljs-keyword">let</span> updateQueue = workInProgress.updateQueue;
  <span class="hljs-keyword">if</span> (updateQueue !== <span class="hljs-literal">null</span>) {
    <span class="hljs-keyword">const</span> nextProps = workInProgress.pendingProps;
    <span class="hljs-keyword">const</span> prevState = workInProgress.memoizedState;
    <span class="hljs-keyword">const</span> prevChildren = prevState !== <span class="hljs-literal">null</span> ? prevState.element : <span class="hljs-literal">null</span>;
    processUpdateQueue(
      workInProgress,
      updateQueue,
      nextProps,
      <span class="hljs-literal">null</span>,
      renderExpirationTime,
    );
    <span class="hljs-keyword">const</span> nextState = workInProgress.memoizedState;
    <span class="hljs-comment">// Caution: React DevTools currently depends on this property</span>
    <span class="hljs-comment">// being called &quot;element&quot;.</span>
    <span class="hljs-keyword">const</span> nextChildren = nextState.element;

    <span class="hljs-keyword">if</span> (nextChildren === prevChildren) {
      <span class="hljs-comment">// If the state is the same as before, that&apos;s a bailout because we had</span>
      <span class="hljs-comment">// no work that expires at this time.</span>
      resetHydrationState();
      <span class="hljs-keyword">return</span> bailoutOnAlreadyFinishedWork(current, workInProgress);
    }
    <span class="hljs-keyword">const</span> root: FiberRoot = workInProgress.stateNode;
    <span class="hljs-keyword">if</span> (
      (current === <span class="hljs-literal">null</span> || current.child === <span class="hljs-literal">null</span>) &amp;&amp;
      root.hydrate &amp;&amp;
      enterHydrationState(workInProgress)
    ) {
      workInProgress.effectTag |= Placement;

      workInProgress.child = mountChildFibers(
        workInProgress,
        <span class="hljs-literal">null</span>,
        nextChildren,
        renderExpirationTime,
      );
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-comment">// Otherwise reset hydration state in case we aborted and resumed another</span>
      <span class="hljs-comment">// root.</span>
      resetHydrationState();
      reconcileChildren(current, workInProgress, nextChildren);
    }
    <span class="hljs-keyword">return</span> workInProgress.child;
  }
  resetHydrationState();
  <span class="hljs-comment">// If there is no update queue, that&apos;s a bailout because the root has no props.</span>
  <span class="hljs-keyword">return</span> bailoutOnAlreadyFinishedWork(current, workInProgress);
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><p><code>pushHostRootContext</code>&#x76EE;&#x524D;&#x610F;&#x4E49;&#x4E0D;&#x660E;&#xFF0C;&#x5148;&#x653E;&#x7740;&#x540E;&#x9762;&#x770B;&#x770B;&#x6709;&#x6CA1;&#x6709;&#x7528;&#x5230;&#x7684;&#x5730;&#x65B9;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js">pushHostRootContext(workInProgress);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pushHostRootContext</span>(<span class="hljs-params">workInProgress</span>) </span>{
  <span class="hljs-keyword">var</span> root = workInProgress.stateNode;
  <span class="hljs-keyword">if</span> (root.pendingContext) {
    pushTopLevelContextObject(workInProgress, root.pendingContext, root.pendingContext !== root.context);
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (root.context) {
    <span class="hljs-comment">// Should always be set</span>
    pushTopLevelContextObject(workInProgress, root.context, <span class="hljs-literal">false</span>);
  }
  pushHostContainer(workInProgress, root.containerInfo);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pushTopLevelContextObject</span>(<span class="hljs-params">fiber, context, didChange</span>) </span>{
  !(contextStackCursor.current === emptyObject) ? invariant(<span class="hljs-literal">false</span>, <span class="hljs-string">&apos;Unexpected context found on stack. This error is likely caused by a bug in React. Please file an issue.&apos;</span>) : <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>;

  push(contextStackCursor, context, fiber);
  push(didPerformWorkStackCursor, didChange, fiber);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">push</span>(<span class="hljs-params">cursor, value, fiber</span>) </span>{
  index++;

  valueStack[index] = cursor.current;

  {
    fiberStack[index] = fiber;
  }

  cursor.current = value;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pushHostContainer</span>(<span class="hljs-params">fiber, nextRootInstance</span>) </span>{
  <span class="hljs-comment">// Push current root instance onto the stack;</span>
  <span class="hljs-comment">// This allows us to reset root when portals are popped.</span>
  push(rootInstanceStackCursor, nextRootInstance, fiber);
  <span class="hljs-comment">// Track the context and the Fiber that provided it.</span>
  <span class="hljs-comment">// This enables us to pop only Fibers that provide unique contexts.</span>
  push(contextFiberStackCursor, fiber, fiber);

  <span class="hljs-comment">// Finally, we need to push the host context to the stack.</span>
  <span class="hljs-comment">// However, we can&apos;t just call getRootHostContext() and push it because</span>
  <span class="hljs-comment">// we&apos;d have a different number of entries on the stack depending on</span>
  <span class="hljs-comment">// whether getRootHostContext() throws somewhere in renderer code or not.</span>
  <span class="hljs-comment">// So we push an empty value first. This lets us safely unwind on errors.</span>
  push(contextStackCursor$<span class="hljs-number">1</span>, NO_CONTEXT, fiber);
  <span class="hljs-keyword">var</span> nextRootContext = getRootHostContext(nextRootInstance);
  <span class="hljs-comment">// Now that we know this function doesn&apos;t throw, replace it.</span>
  pop(contextStackCursor$<span class="hljs-number">1</span>, fiber);
  push(contextStackCursor$<span class="hljs-number">1</span>, nextRootContext, fiber);
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="processupdatequeue">processUpdateQueue</h1><p><code>getStateFromUpdate</code>&#x4ECE;<code>update</code>&#x5BF9;&#x8C61;&#x4E0A;&#x83B7;&#x53D6;&#x6700;&#x65B0;&#x7684;<code>state</code></p><p>&#x5982;&#x679C;&#x6709;callback&#x5219;&#x8BBE;&#x7F6E;<code>workInProgress.effectTag |= Callback</code>&#xFF0C;<code>Callback</code>&#x662F;&#x4E2A;&#x5E38;&#x6570;&#xFF0C;&#x7B49;&#x4E8E;32&#xFF0C;&#x8DDF;&#x76EE;&#x524D;&#x7684;<code>effectTag</code>&#x8FDB;&#x884C;&#x6216;&#x8FD0;&#x7B97;&#x5F97;&#x5230;&#x65B0;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x7136;&#x540E;&#x8BBE;&#x7F6E;<code>queue</code>&#x7684;<code>effect</code>&#x94FE;</p><p><strong>&#x731C;&#x60F3;&#xFF1A;</strong>&#x5982;&#x679C;&#x6CA1;&#x6709;callback&#x4EE3;&#x8868;&#x6CA1;&#x6709;<code>effect</code></p><p>&#x4E4B;&#x540E;&#x5FAA;&#x73AF;<code>capturedUpdate</code>&#xFF0C;&#x4E0D;&#x592A;&#x6E05;&#x695A;&#x8FD9;&#x4E2A;<code>update</code>&#x662F;&#x5E72;&#x561B;&#x7684;</p><p>TODO: &#x4EC0;&#x4E48;&#x662F;<code>capturedUpdate</code></p><p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x4E3B;&#x8981;&#x4F5C;&#x7528;&#x662F;&#x5904;&#x7406;<code>updateQueue</code>&#x91CC;&#x9762;&#x7684;update&#xFF0C;&#x6267;&#x884C;&#x5E76;&#x83B7;&#x5F97;&#x6700;&#x65B0;&#x7684;state&#xFF0C;&#x6700;&#x540E;&#x83B7;&#x53D6;<code>effect</code>&#x653E;&#x7F6E;&#x5230;<code>Fiber</code>&#x5BF9;&#x8C61;&#x4E0A;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-keyword">var</span> _callback = update.callback;
<span class="hljs-keyword">if</span> (_callback !== <span class="hljs-literal">null</span>) {
  workInProgress.effectTag |= Callback;
  <span class="hljs-comment">// Set this to null, in case it was mutated during an aborted render.</span>
  update.nextEffect = <span class="hljs-literal">null</span>;
  <span class="hljs-keyword">if</span> (queue.lastEffect === <span class="hljs-literal">null</span>) {
    queue.firstEffect = queue.lastEffect = update;
  } <span class="hljs-keyword">else</span> {
    queue.lastEffect.nextEffect = update;
    queue.lastEffect = update;
  }
}

<span class="hljs-comment">// Separately, iterate though the list of captured updates.</span>
  <span class="hljs-keyword">var</span> newFirstCapturedUpdate = <span class="hljs-literal">null</span>;
  update = queue.firstCapturedUpdate;
  <span class="hljs-keyword">while</span> (update !== <span class="hljs-literal">null</span>) {
    <span class="hljs-keyword">var</span> _updateExpirationTime = update.expirationTime;
    <span class="hljs-keyword">if</span> (_updateExpirationTime &gt; renderExpirationTime) {
      <span class="hljs-comment">// This update does not have sufficient priority. Skip it.</span>
      <span class="hljs-keyword">if</span> (newFirstCapturedUpdate === <span class="hljs-literal">null</span>) {
        <span class="hljs-comment">// This is the first skipped captured update. It will be the first</span>
        <span class="hljs-comment">// update in the new list.</span>
        newFirstCapturedUpdate = update;
        <span class="hljs-comment">// If this is the first update that was skipped, the current result is</span>
        <span class="hljs-comment">// the new base state.</span>
        <span class="hljs-keyword">if</span> (newFirstUpdate === <span class="hljs-literal">null</span>) {
          newBaseState = resultState;
        }
      }
      <span class="hljs-comment">// Since this update will remain in the list, update the remaining</span>
      <span class="hljs-comment">// expiration time.</span>
      <span class="hljs-keyword">if</span> (newExpirationTime === NoWork || newExpirationTime &gt; _updateExpirationTime) {
        newExpirationTime = _updateExpirationTime;
      }
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-comment">// This update does have sufficient priority. Process it and compute</span>
      <span class="hljs-comment">// a new result.</span>
      resultState = getStateFromUpdate(workInProgress, queue, update, resultState, props, instance);
      <span class="hljs-keyword">var</span> _callback2 = update.callback;
      <span class="hljs-keyword">if</span> (_callback2 !== <span class="hljs-literal">null</span>) {
        workInProgress.effectTag |= Callback;
        <span class="hljs-comment">// Set this to null, in case it was mutated during an aborted render.</span>
        update.nextEffect = <span class="hljs-literal">null</span>;
        <span class="hljs-keyword">if</span> (queue.lastCapturedEffect === <span class="hljs-literal">null</span>) {
          queue.firstCapturedEffect = queue.lastCapturedEffect = update;
        } <span class="hljs-keyword">else</span> {
          queue.lastCapturedEffect.nextEffect = update;
          queue.lastCapturedEffect = update;
        }
      }
    }
    update = update.next;
  }</code></pre>
</div></div></div>




                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
<div id="disqus_thread"></div>
<script>
  /**
   *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
   *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
  /*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
  ;(function() {
    // DON'T EDIT BELOW THIS LINE
    var d = document,
      s = d.createElement('script')
    s.src = 'https://react-source-code.disqus.com/embed.js'
    s.setAttribute('data-timestamp', +new Date())
    ;(d.head || d.body).appendChild(s)
  })()
</script>
<noscript
  >Please enable JavaScript to view the
  <a href="https://disqus.com/?ref_noscript"
    >comments powered by Disqus.</a
  ></noscript
>

            </div>

            

        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"updateHostRoot","level":"1.4.7.5","depth":3,"next":{"title":"updateHostComponent","level":"1.4.7.6","depth":3,"path":"book/flow/begin-work/update-host-component.md","ref":"book/flow/begin-work/update-host-component.md","articles":[]},"previous":{"title":"updateClassComponent","level":"1.4.7.4","depth":3,"path":"book/flow/begin-work/update-class-component.md","ref":"book/flow/begin-work/update-class-component.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-api"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["weibo","google","twitter","weibo","instapaper"]},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-api":{"languages":[],"split":true,"theme":"light"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React源码解析","language":"zh-hans","gitbook":"*"},"file":{"path":"book/flow/begin-work/update-host-root.md","mtime":"2018-10-09T08:13:36.745Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2019-07-08T12:28:32.264Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-theme-api/theme-api.js"></script>
        
    

    </body>
</html>

